package com.eslserver.action.schedule;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eslserver.core.ESLServerPageDispatcher;
import com.eslserver.core.ScheduleTimerInitializer;
import com.eslserver.core.aware.ScheduleAware;
import com.eslserver.dao.DAOException;
import com.eslserver.dao.ESLDAO;
import com.eslserver.dao.ESLServerDAOFactory;
import com.eslserver.dao.ImageDAO;
import com.eslserver.dao.ScheduleDAO;
import com.eslserver.to.ESL;
import com.eslserver.to.Image;
import com.eslserver.to.Schedule;
import com.eslserver.util.FormatUtil;

@SuppressWarnings("serial")
public class AddSchedule extends HttpServlet implements ScheduleAware,
		ESLServerPageDispatcher {

	public static final Logger LOG = LoggerFactory.getLogger(AddSchedule.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			Integer scheduleID = Integer.valueOf(req
					.getParameter(PARAM_SCHEDULE_ID));
			Integer imageID = null;
			Integer eslID = null;

			ESLDAO eslDAO = ESLServerDAOFactory.getDAOFactory(
					ESLServerDAOFactory.ESLSERVER_DAOFACTORY).getESLDAO();
			ImageDAO imageDAO = ESLServerDAOFactory.getDAOFactory(
					ESLServerDAOFactory.ESLSERVER_DAOFACTORY).getImageDAO();
			ScheduleDAO scheduleDAO = ESLServerDAOFactory.getDAOFactory(
					ESLServerDAOFactory.ESLSERVER_DAOFACTORY).getScheduleDAO();

			Schedule schedule = null;
			Date now = new Date();
			if (scheduleID.equals(0)) {
				schedule = Schedule.createSchedule();
				schedule.setCreateDate(now);
				schedule.setModifyDate(now);
				schedule.setDelete(false);
			} else {
				schedule = scheduleDAO.loadSchedule(scheduleID);
			}

			if (req.getParameter(PARAM_SCHEDULE_NAME) != null
					&& req.getParameter(PARAM_SCHEDULE_NAME).length() > 0) {
				schedule.setName(req.getParameter(PARAM_SCHEDULE_NAME));
			}
			if (req.getParameter(PARAM_ESL_IMAGE) != null
					&& req.getParameter(PARAM_ESL_IMAGE).length() > 0) {
				imageID = Integer.valueOf(req.getParameter(PARAM_ESL_IMAGE));
				Image image = imageDAO.loadImageByImageID(imageID);
				schedule.setImage(image);
			}
			if (req.getParameter(PARAM_ESL) != null
					&& req.getParameter(PARAM_ESL).length() > 0) {
				eslID = Integer.valueOf(req.getParameter(PARAM_ESL));
				ESL esl = eslDAO.loadESL(eslID);
				schedule.setEsl(esl);
			}
			if (req.getParameter(PARAM_START_DATE) != null
					&& req.getParameter(PARAM_START_TIME) != null) {
				try {
					String dateStr = req.getParameter(PARAM_START_DATE) + "-"
							+ req.getParameter(PARAM_START_TIME);
					Date startDate = FormatUtil.getUSDateFormatTimestamp()
							.parse(dateStr);
					schedule.setStartTime(startDate);
				} catch (ParseException e) {
					LOG.error(e.getMessage(), e);
				}
			}
			if (req.getParameter(PARAM_REPEAT) != null
					&& req.getParameter(PARAM_REPEAT).length() > 0) {
				schedule.setRepeat(true);
			} else {
				schedule.setRepeat(false);
			}
			if (req.getParameter(PARAM_TIME_INTERVAL) != null
					&& req.getParameter(PARAM_TIME_INTERVAL).length() > 0) {
				schedule.setTimeInterval(Integer.parseInt(req
						.getParameter(PARAM_TIME_INTERVAL)));
			}
			if (req.getParameter(PARAM_SCHEDULE_DESCRIPTION) != null) {
				schedule.setDescription(req
						.getParameter(PARAM_SCHEDULE_DESCRIPTION));
			}

			if (scheduleID.equals(0)) {
				scheduleDAO.addSchedule(schedule);
				scheduleID = schedule.getScheduleID();
			} else {
				scheduleDAO.updateSchedule(schedule);
			}

			ScheduleTimerInitializer.addScheduleTimer(schedule);

			req.setAttribute(ATTRIBUTE_TAB_ID, SCHEDULE_TAB_ID);
			RequestDispatcher dispatcher = req
					.getRequestDispatcher(MAIN_DASHBOARD);
			dispatcher.forward(req, resp);

		} catch (DAOException e) {
			LOG.error("Error to add schedule.", e);
		}
	}

}
